package tests;

import static org.junit.Assert.*;

import java.util.Scanner;

import entities.Inventory;
import fields.Fleet;
import fields.LaborCamp;
import fields.Ownable;
import fields.Territory;
import gstd.Field;
import gstd.GameFlow;
import gstd.GameManagerAbs;

import org.junit.Test;

public class TestCaseBankrupt {
	int turnCounter = 0;
	
//TEST SCRIPT IS UNSUCCESSFUL	
	@Test
	public void testBankrupt() {
		GameManagerWithoutUI gameManager = new GameManagerWithoutUI();

		gameManager.initialize();
		
		Inventory playerOneInventory = (Inventory)gameManager.getPlayerList().getPlayer(0).getInventory();
		Inventory playerTwoInventory = (Inventory)gameManager.getPlayerList().getPlayer(1).getInventory();
		Inventory playerThreeInventory = (Inventory)gameManager.getPlayerList().getPlayer(2).getInventory();		
		Inventory playerFourInventory = (Inventory)gameManager.getPlayerList().getPlayer(3).getInventory();		
		
		while (true) {
			gameManager.match();
			//Preconditions

			//Adds fields to the players' inventory
			//Gives player 1 field 0
			((Ownable)gameManager.getGameBoard().getField(0)).setOwner(gameManager.getPlayerList().getPlayer(0));
			playerOneInventory.addField(gameManager.getGameBoard().getField(0));
			
			//Gives player 2 field 1
			((Ownable)gameManager.getGameBoard().getField(1)).setOwner(gameManager.getPlayerList().getPlayer(1));
			playerTwoInventory.addField(gameManager.getGameBoard().getField(1));
			
			//Gives player 3 field 4
			((Ownable)gameManager.getGameBoard().getField(4)).setOwner(gameManager.getPlayerList().getPlayer(2));
			playerThreeInventory.addField(gameManager.getGameBoard().getField(4));
			
			//Gives player 4 field 7
			((Ownable)gameManager.getGameBoard().getField(7)).setOwner(gameManager.getPlayerList().getPlayer(3));
			playerFourInventory.addField(gameManager.getGameBoard().getField(7));
			
			System.out.println("Player1: " + playerOneInventory.getOwnedFields());
			System.out.println("Player2: " + playerTwoInventory.getOwnedFields());
			System.out.println("Player3: " + playerThreeInventory.getOwnedFields());
			System.out.println("Player4: " + playerFourInventory.getOwnedFields());
			

			//
			playerOneInventory.addBalance(10000);
			playerTwoInventory.addBalance(10000);
			playerThreeInventory.addBalance(10000);
			playerFourInventory.addBalance(10000);

			//Test
			do {
				gameManager.round();
				do {
					
					gameManager.turn();
					
					//Adds -100000 to the players' balance
					if (gameManager.getTurnCounter() == 4) {
						gameManager.currentPlayer.getInventory().addBalance(-100000);
						if(gameManager.checkBankruptcy(gameManager.getPlayerList().getPlayers()[0])){
							((Inventory)gameManager.getPlayerList().getPlayers()[0].getInventory()).reset();
						}
						
					}
					if (gameManager.getTurnCounter() == 5) {
						gameManager.currentPlayer.getInventory().addBalance(-100000);
						gameManager.checkBankruptcy(gameManager.getPlayerList().getPlayers()[1]);
						((Inventory)gameManager.getPlayerList().getPlayers()[0].getInventory()).reset();
					}
					if (gameManager.getTurnCounter() == 6) {
						gameManager.currentPlayer.getInventory().addBalance(-100000);
						gameManager.checkBankruptcy(gameManager.getPlayerList().getPlayers()[2]);
						((Inventory)gameManager.getPlayerList().getPlayers()[0].getInventory()).reset();
					}
					
					System.out.println(playerOneInventory.getOwnedFields().size());
					
					turnCounter++;
					System.out.println("Turn: " + turnCounter);
					for (int i = 0; i < gameManager.getPlayerList().getPlayers().length; i++) {
						System.out.println("Player " + (i + 1) + " inventory: " + gameManager.getPlayerList().getPlayers()[i].getInventory().getBalance() + 
								" total assets: " + ((Inventory) gameManager.getPlayerList().getPlayers()[i].getInventory()).getTotalAssets() + 
								" number of owned fields: " + playerOneInventory.getOwnedFields().size());
					}
					

				} while (!gameManager.isRoundDone());

				
				gameManager.endRound();
			} while (!gameManager.isMatchDone());
			
			gameManager.endMatch();
			
			//Post Conditions
			//If the match ends, break. The game has found 3 bankrupt players out of 4 and therefore ends the gameBoard
			System.out.println(playerOneInventory.getOwnedFields());
			System.out.println(playerTwoInventory.getOwnedFields());
			System.out.println(playerThreeInventory.getOwnedFields());
			System.out.println(playerFourInventory.getOwnedFields());
			System.out.println("End");

			
			break;

		}
	}
}
